var vm=new Vue({
	el:"#app",
	data :{
		protos :[],
		filterProtos :[],
		curSelectProto: 0,
		protoFilter:'',
		protoArg:'协议参数:',
		protoRes:'等待发送...',
		onlineUsers:[],
		curSelectProtoName: '',
		curSelectRoleId : 0,
	},
	methods: {
		cmdRefresh: function(){
			if (localStorage.getItem("protoFilter") != null) {
                this.protoFilter = localStorage.getItem("protoFilter");
            }
            $.ajax({
                url: "/protos",
                method: "GET",
                success: function(data){
                	vm.protos=data.protos
                	vm.cmdFilterProtocol()
                }
            })
		},
		cmdProtocolColor : function(idx){
			var colors=["table-active","table-success","table-warning","table-danger"]
			if (idx >= colors.length) {
				idx %= colors.length
			}
			return colors[idx]
		},
		cmdSelectProtocol: function(protoId, protoName) {
			this.curSelectProto = protoId
			if (protoName.length > 0) {
				this.curSelectProtoName = protoName
			}
			$.ajax({
				url: "/genproto?protoId="+protoId,
				method: "GET",
				success: function(data){
					vm.protoArg = data
				}
			})
		},
		cmdFilterProtocol: function() {
			localStorage.setItem("protoFilter", this.protoFilter)
			if (this.protoFilter.length == 0) {
				this.filterProtos = this.protos
				return
			}
			var filter = new RegExp(this.protoFilter, 'i')
			this.filterProtos = []
			for (var idx in this.protos) {
				if (this.protos[idx].ProtoName.search(filter) != -1){
					this.filterProtos.push(this.protos[idx])
				}
			}
		},
		cmdRefreshProtolParam: function() {
			if (this.curSelectProto == 0) {
				return
			}
			this.cmdSelectProtocol(this.curSelectProto, "")
		},
		cmdSendMsg: function () {
			var sendData = {
				"RoleUid" : vm.curSelectRoleId,
				"ProtoId" : vm.curSelectProto,
				"MsgBody" : vm.protoArg,
			}
			vm.protoRes = "准备发送..."
			$.ajax({
				type : "POST",
				url : "/send",
				data : sendData,
				success : function (result) {
					vm.protoRes = result.msg
				}, 
				error: function () {
					vm.protoRes = "发送失败..."
				}
			})
		},
		cmdQueryRoleList: function() {
			$.ajax({
                url: "/users",
                method: "GET",
                success: function(data){
                	vm.onlineUsers=data.users
                	if (vm.onlineUsers == null) {
                		vm.onlineUsers = []
                	}
                }
            })
		},
	},
	mounted: function() {
		this.cmdRefresh()
	}
})
